/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package spring;

import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.util.*;



/**
 *
 * @author spring
 */
public class POIBut extends javax.swing.JButton{
    PlotPanelPOI parent;
    java.awt.Color bg;
    boolean active=true;
    boolean chloc=false;
    double mx=0.0, my=0.0;
    HashMap poihm;
    int cw=0, ch=0;
    int num=-1;
    double[] dvec;
    
    public POIBut(){
        super();
        this.setBounds(0,0,60,60);
        java.awt.Insets in = new java.awt.Insets(0,0,0,0);
        javax.swing.border.EmptyBorder eb = new javax.swing.border.EmptyBorder(in);
        this.setBorder(eb);
        bg = this.getBackground();
        this.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        this.setBackground(new Color(100,255,100));
        this.addActionListener(new ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent e) {
            btnpress();  // code to execute when button is pressed
        }});
    }
    
    public void setcoords(PlotPanelPOI p, double x, double y){
       parent=p;
       cw = parent.getWidth();
       ch = parent.getHeight();
       mx=x;
       my=y;
       this.setLocation((int)((mx*cw/2)+(cw/2)-30),(int)((my*ch/2)+(ch/2)-30));
       // System.out.println("loc"+mx+","+my);
    }
    public void setNum(int i){
        num=i;
    }
    public int getNum(){
        return num;
    }
    public void btnpress() {
        //System.out.println("x = "+mx+", y = "+my);
        if (chloc){chloc=false;return;}
        
        //for (int i=0;i<vectors.length;i++)
        //    System.out.println(vectors[i]);
        if (active){ 
            active=false;
            bg=this.getBackground();
            this.setBackground(new Color(212,212,212));
        }
        else {
            active = true;
            this.setBackground(bg);
        }
        ((PlotPanelPOI)this.getParent()).replotDB();
    }
    
    public boolean isActive(){
        return active;
    }
    public HashMap getPoiHashMap(){
        return poihm;
    }
    
    /* sets the meaning of a poi to a POI object. converts a hashmap of information
     attributes into a string, and attaches this string to a POI object*/    
    public void setPoiHashMap(HashMap in){
        poihm=in;
        Set kc = poihm.keySet();
        Iterator it = kc.iterator();
        String tt = new String(this.getText()+": ");
        while (it.hasNext()){
            tt+=(String)it.next()+",";
        }
        this.setToolTipText(tt); 
    }
    public void setdoubleVector(double[] in){
        dvec = in;
    }
    
    public double[] getdoubleVector(){
        return dvec;
    }
    
    public double[] getcoords(){
       double[]mp=new double[2];
       mp[0]=mx;
       mp[1]=my;
       return mp;
    }
    
    public void parentresize(int w, int h){
        cw=w;
        ch=h;
        this.setLocation((int)((mx*cw/2)+(cw/2)-30),(int)((my*ch/2)+(ch/2)-30));
    }
    
    
        
   

    @Override
    protected void processMouseMotionEvent(MouseEvent e) {
        if (e.CONTAINER_EVENT_MASK == java.awt.event.MouseEvent.CONTAINER_EVENT_MASK &&
               (e.getID() == java.awt.event.MouseEvent.MOUSE_PRESSED ||
               e.getID() == java.awt.event.MouseEvent.MOUSE_DRAGGED)){
            Point p = this.getLocation();
            cw = this.getParent().getWidth();
            ch = this.getParent().getHeight();
            this.setcoords(parent, (((e.getX()) + p.getX())-cw/2)/(cw/2.0),
                    (((e.getY()) + p.getY())-ch/2)/(ch/2.0));
            ((PlotPanelPOI)this.getParent()).replotDB();
            chloc=true;
            e.consume();
        }
        else {super.processMouseMotionEvent(e);}
    } 
   
}
